home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeeder.netgate.net!usenet
- From: Tamara Johnson <malihini@netgate.net>
- Newsgroups: comp.lang.c
- Subject: (no subject)
- Date: 21 Apr 1996 19:42:33 GMT
- Organization: NetGate Communications
- Message-ID: <4le339$95a@ss.netgate.net>
- NNTP-Posting-Host: d81.netgate.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- /* Problem: Possible to pass a whole struct to
- a function?
-
- Requirement: create a function that will
- printf a union regardless of type (using a flag)
- using one struct containing the union and flag
- (and etc.).
-
- Don't need the solution, just need help passing the
- the struct and how to printf (point to?) individual
- elements within. I *think* I can get the rest of it */
-
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- void Function1( void *P1, void *P2);
- void Function2( void *P1);
-
-
- /* main ----------------------------- */
- void main()
- {
- union tag_1{
- float f;
- int i;
- char string[5];
- double d;
- };
-
- struct tag_2{
- union tag_1 Union;
- int flag;
- }Flag, *fP;
-
- /* stuff to figure out if it's working to this point */
- Flag.flag=3;
- Flag.Union.f=5.3456;
- /* conversion from 'const double '
- to 'float ', possible loss of data
- NOTE: works ok */
- Flag.Union.d=78.9631;
- Flag.Union.i=31;
- strcpy(Flag.Union.string,"red");
-
- printf("main() Flag.Union.string: %s\n",
- Flag.Union.string);
- printf("main() Flag.Union.i : %i\n", Flag.Union.i);
- printf("main() Flag.Union.f : %f\n", Flag.Union.f);
- printf("main() Flag.Union.d : %f\n", Flag.Union.d);
-
- printf("main() Flag.flag : %i\n", Flag.flag);
-
- /* end of stuff - is working when Function2 is deleted.
- Of course, whatever union member is initialized last
- is the one that follows thru, others will be
- essentially garbage */
-
- Function1(Flag.Union.string, Flag.flag); /* Function1'
- :pointer mismatch for actual parameter 2
- NOTE: but appears to be functioning ok
- when Function2 deleted.
- except passing individual elements,
- need to pass the whole struct */
- /*
- Function2(fP);
- */
- }
- /* Function1 ------------------------------ */
-
- void Function1( void *P1, void *P2)
- {
-
- printf("Function1 P1: %s\n", ( (char *)P1));
- printf("Function1 P2: %i\n", ( (int *)P2));
-
- }
- /* Function2
-
- void Function2( void *P1) /*NOTE: not working
- {
- /*
- printf("Function2 output:\n %s\n",
- ( (char *)P1->tag_1));
-
- printf("Function2\n");
- if(( (char *)P1).flag==3) /* left of '.f' must have
- struct/union type
- NOTE: not working
- printf("Function2 output:\n %s\n",
- ( (char *)P1->Union.string));
- /* left of '->Union' must point to
- struct/union
- NOTE: not working
- }
- ------------------------------ */
- /* ------------------ OUTPUT ---------------------------
- main() Flag.Union.string: red
- main() Flag.Union.i : 6579570
- main() Flag.Union.f : 0.000000
- main() Flag.Union.d : 78.963074
- main() Flag.flag : 3
- Function1 P1: red
- Function1 P2: 3
-
- ----------------------------------------------------- */
-
-
-